﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RadarMonitor.DB;
using RadarMonitor;

namespace RadarMonitor.ViewModel 
{
   public class StatusMonitorViewModel : INotifyPropertyChanged
    {
        public StatusMonitorViewModel()
        {
            try
            {
                //var record =  DBHelper.Instance.lastedStatusAlarmRecord();
                //LastRecord = record;
            }
            catch { }
            StatusMonitor.StatusMonitor.Instance.RDAAlarmEvent += Instance_RDAAlarmEvent;
            StatusMonitor.StatusMonitor.Instance.RDAComunicationEevent += Instance_RDAComunicationEevent;

        }
        private Setting.StatusConfig config;
        private void Instance_RDAComunicationEevent(bool alive)
        {
            config = Setting.Setting.GetConfig().status;
            if(IsRDARunning && !alive)
            {
                DBHelper.Instance.insertMonidorRow((int)RadarMonitorSource.RDA_STATUS, (int)RadarMonitorResultCode.RDA_CommnunicationFailed, null);
                Alarm.AlarmManager.Alarm(RadarMonitorSource.RDA_STATUS, RadarMonitorResultCode.RDA_CommnunicationFailed, null, config.DingDingEnabel, config.SmsEnabel, config.AudioEnabel);
            }
            IsRDARunning = alive;
        }

        private void Instance_RDAAlarmEvent(string record)
        {
            LastRecord = record;
            DBHelper.Instance.insertMonidorRow((int)RadarMonitorSource.RDA_STATUS, (int)RadarMonitorResultCode.RDA_Alram, record);
            Alarm.AlarmManager.Alarm(RadarMonitorSource.RDA_STATUS, RadarMonitorResultCode.RDA_Alram, record, config.DingDingEnabel, config.SmsEnabel, config.AudioEnabel);
            
        }

        #region DataContext

        private bool _isWorking;

        public bool IsWorking
        {
            get { return _isWorking; }
            set
            {
                _isWorking = value;
                OnPropertyChanged("IsWorking");
            }
        }

        private bool _isRDARunning;

        public bool IsRDARunning
        {
            get { return _isRDARunning; }
            set
            {
                _isRDARunning = value;
                OnPropertyChanged("IsRDARunning");
            }
        }

        private string _LastRecord;

        public string LastRecord
        {
            get { return _LastRecord; }
            set { _LastRecord = value;
                OnPropertyChanged("LastRecord");
            }
        }
        #endregion

        #region Action
        public void start()
        {
            StatusMonitor.StatusMonitor.Instance.Start();
            IsWorking = true;
            IsRDARunning = true;
        }

        public void stop()
        {
            StatusMonitor.StatusMonitor.Instance.Stop();
            IsWorking = false;
        }


        public void toogle()
        {
            if (IsWorking)
            {
                stop();
            }
            else
            {
                start();
            }
        }

        #endregion



        #region 属性变化通知
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName = "")
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }



        #endregion
    }
}
